package com.ang.enums.t1;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

@Getter
public enum StatusEnum {
    NORMAL("正常","normal"),
    ENABLE("可用","enable"),
    DISABLE("禁用","disable");

    private String desc;

    private String code;

    StatusEnum(String desc, String code) {
        this.desc = desc;
        this.code = code;
    }

    /**
     * 根据code获取desc
     * @param code
     * @return
     */
    public static String getDescByCode(String code){
        Optional<StatusEnum> any = Arrays.stream(StatusEnum.values())
                .filter(statusEnum -> StringUtils.equals(statusEnum.code, code))
                .findAny();

        if(any.isPresent()) {
            return any.get().desc;
        }
        return null;
    }

    /**
     * NORMAL，ENABLE，DISABLE
     * @param name
     * @return
     */
    public static StatusEnum getByName(String name){
        if (StrUtil.isNotBlank(name)) {
            try {
                return valueOf(name.trim());
            } catch (IllegalArgumentException e) {
                System.out.println("标识有误");
            }
        }
        return null;
    }

    /**
     *
     * @param code
     * @return
     */
    public static StatusEnum getByCode(String code){
        Optional<StatusEnum> any = Arrays.stream(StatusEnum.values())
                .filter(statusEnum -> StringUtils.equals(statusEnum.code, code))
                .findAny();

        if(any.isPresent()) {
            return any.get();
        }
        return null;
    }
}
